#include "ModelRenderer.h"
#include "../geom/PolyModel.h"
#include "FreeCamera.h"

using namespace glutil;

ModelRenderer::ModelRenderer(const geom::PolyModel& model, float baseScale)
{
	m_baseScale = baseScale;
	for(int i=0;i<model.numberOfMeshes();++i)
		m_meshRenderers.push_back(MeshRenderer(&model.getMesh(i)));
}

void ModelRenderer::addInstance(const GLInstanceInfo & m)
{
	for(unsigned int i=0;i<m_meshRenderers.size();++i)
		m_meshRenderers[i].addInstance(m);
}

void ModelRenderer::clearInstances()
{
	for(unsigned int i=0;i<m_meshRenderers.size();++i)
		m_meshRenderers[i].clearInstances();
}

void ModelRenderer::render(int instance, bool withMaterials)
{
	for(unsigned int i=0;i<m_meshRenderers.size();++i)
		m_meshRenderers[i].render(instance, withMaterials);
}

void ModelRenderer::renderAll(bool withMaterials)
{
	for(unsigned int i=0;i<m_meshRenderers.size();++i)
		m_meshRenderers[i].renderAll(withMaterials);
}

void ModelRenderer::renderAll(bool withMaterials, const glutil::FreeCamera * camera, float maxDistance, int selectedTree)
{
	for(unsigned int i=0;i<m_meshRenderers.size();++i)
		m_meshRenderers[i].renderAll(withMaterials,camera, maxDistance, selectedTree,m_baseScale);
}

void ModelRenderer::generateVertexBufferObjects()
{
	for(unsigned int i=0;i<m_meshRenderers.size();++i)
		m_meshRenderers[i].generateVertexBufferObjects();
}

ModelRenderer * ModelRenderer::simpleCenteredInstanceCreator(const geom::PolyModel &model)
{
	ModelRenderer * res = new ModelRenderer(model);
	GLInstanceInfo m;
	m.m_scale = 1.0;
/*	
	for(int i=0;i<6;++i)
		for(int j=0;j<6;++j)
		{
			m.m_location[0] = i + sin(i*j*30.0)*2/3.0 - 3;
			m.m_location[1] = 0;
			m.m_location[2] = j + cos(i*j*13.24)*2/3.0 - 3;

			m.m_rotation[0] = 0;
			m.m_rotation[1] = m.m_location[0] + j*i+ j*1139.1231 + i*234.12;
			m.m_rotation[2] = 0;

			res->addInstance(m);
		}
*/
	for(int i=0;i<3;i++)
	{
		m.m_location[i] = 0;
		m.m_rotation[i] = 0;
	}
	res->addInstance(m);
	return res;
}

